package com.kordar.admin.modular.sys.service.setting;

import com.kordar.admin.kernel.util.HtmlUtil;
import com.kordar.admin.modular.sys.entity.SettingEntity;
import com.kordar.auth.Admin;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


public class PersonalProcessor implements ProcessorElement {

    @Override
    public String render(SettingEntity setting, String href, Map<String, String> options) {
        return getString(href, options);
    }

    public static String getString(String href, Map<String, String> options) {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        Admin admin = (Admin) request.getAttribute("admin");
        if (admin == null) {
            return HtmlUtil.a("未登录", href, options);
        }

        Map<String, String> opt = new HashMap<>();
        opt.put("class", "layui-nav-img");
        String img = HtmlUtil.img(admin.getAvatar(), opt);
        return HtmlUtil.layA(img + " <cite>" + admin.getUsername() + "</cite>", "", options);
    }
}
